package com.gitee.zycra.future.job.dao;

import com.gitee.zycra.future.job.common.bo.PageQuery;
import com.gitee.zycra.future.job.common.enums.ScheduleRatioEnum;
import com.gitee.zycra.future.job.common.model.JobScheduleLog;
import com.gitee.zycra.future.job.common.param.PageQueryRunningParam;
import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.jdbc.enums.SQLConditionEnum;
import com.gitee.zycra.jdbc.enums.SQLLinkEnum;
import com.gitee.zycra.jdbc.model.PageQueryWrapper;
import com.gitee.zycra.jdbc.model.PageResult;
import com.gitee.zycra.jdbc.util.SQLBlock;
import com.gitee.zycra.jdbc.util.SQLChain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zycra
 */
@Repository
public class JobScheduleLogDao extends BaseDao<JobScheduleLog> {

    @Autowired
    private CommonSelector commonSelector;

    private static final String SCHEDULE_TIME = "schedule_time";

    /**
     * 批量插入执行日志
     *
     * @param jobScheduleLogList 执行日志集合
     * @return 插入条数
     */
    public int batchInsertJobScheduleLog(List<JobScheduleLog> jobScheduleLogList) {
        return batchInsertIntoTable(jobScheduleLogList, false);
    }

    /**
     * 根据任务调度ID查询
     *
     * @param pageQuery 查询参数
     * @return 查询到的数据
     */
    public PageResult<JobScheduleLog> selectJobScheduleLog(PageQuery<PageQueryRunningParam> pageQuery) {
        PageQueryRunningParam queryParam = pageQuery.getParam();
        SQLChain sqlChain = SQLChain.builder()
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "job_name", SQLConditionEnum.CONTAINS, queryParam.getJobName()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "schedule_name", SQLConditionEnum.CONTAINS, queryParam.getScheduleName()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, SCHEDULE_TIME, SQLConditionEnum.GREATER_OR_EQUALS_THAN,
                        queryParam.getScheduleTimeStart()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, SCHEDULE_TIME, SQLConditionEnum.LESS_THAN, queryParam.getScheduleTimeEnd()))
                .build();
        PageQueryWrapper param = new PageQueryWrapper(getCountSqlWithOutParam() + sqlChain.getSQL(),
                sqlChain.getParamList(), getSelectAllColumnSql() + sqlChain.getSQL() + " ORDER BY schedule_time DESC",
                sqlChain.getParamList(), pageQuery.getPage(), pageQuery.getSize());
        return selectTableForPage(param);
    }

    /**
     * 根据调度配置ID集合查询最近周期内完成的调度
     *
     * @param jobScheduleIdList 调度配置ID集合
     * @param startTime         起始时间
     * @return 完成的调度
     */
    public List<Long> selectSuccessByIdList(List<Long> jobScheduleIdList, LocalDateTime startTime) {
        SQLChain sqlChain = SQLChain.builder()
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "job_schedule_id", SQLConditionEnum.IN, jobScheduleIdList.toArray()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "schedule_ratio", SQLConditionEnum.EQUALS, ScheduleRatioEnum.EXECUTE_SUCCESS.getCode()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, SCHEDULE_TIME, SQLConditionEnum.GREATER_THAN, startTime))
                .build();
        return commonSelector.selectByParam(getJdbcTemplate(),
                "SELECT job_schedule_id FROM job_schedule_log" + sqlChain.getSQL(), Long.class, sqlChain.getParamList(), false);
    }
}
